package xj.graph2d.command;

import java.awt.geom.Rectangle2D;
import java.util.Iterator;

import xj.graph2d.DrawShape;
import xj.graph2d.Group;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.SimpleDrawView;

public class PasteGroupCommand extends AbstractCommand {

  public PasteGroupCommand(SimpleDrawView view, SimpleDrawDocument doc,
      Group group) {
    super(view, doc);
    this.group = group;
  }

  public void execute() {
    if (group != null) {
      DrawShape selected = doc.getSelectedShape();
      doc.setSelectedShape(null);
      canvas.repaintShape(selected);
      doc.ungroup(group, false);
      canvas.repaintShape(group);
    }
  }

  public void undo() {
    if (group != null) {
      Rectangle2D r2;
      r2 = group.getRepaintRect();

      Iterator iter = group.allShapes();
      while (iter.hasNext()) {
	DrawShape shape = (DrawShape) iter.next();
	if (shape != null) {
	  doc.remove(shape);
	}
      }
      canvas.repaintRect(r2, true, true);
    }
  }

  @Override
  public String toString() {
    /*
     * String s = ""; if (group != null) { if (shape instanceof TextShape) {
     * TextShape text = (TextShape) shape; s = (text.getName() + " \"" +
     * text.getString() + "\""); } else if (shape instanceof Group) { s =
     * shape.toString(); } else { s = (shape.getName() + " " +
     * shape.getBoundsString()); } }
     */
    // return (getClass().getName() + ": " + s);
    return (getClass().getName());
  }

  protected Group group;

}
